package com.zego.wawaji.ui.dialog.pay;

import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.ui.base.recycler.BaseRecyclerViewAdapter;
import com.zego.wawaji.ui.base.recycler.BaseRecyclerViewHolder;
import com.zego.wawaji.ui.bean.pay.Pay;
import com.zego.wawaji.ui.coupon.bean.Coupon;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;
import com.zego.wawaji.wawaji_client.AppTinkerLike;

/**
 * 支付列表
 * Created by Su on 2017/12/26.
 */
public class PayDialogAdapter extends BaseRecyclerViewAdapter<Pay> {
    private int selectPosition = 0;
    private Coupon coupon;

    public void setCoupon(Coupon coupon) {
        this.coupon = coupon;
    }

    @Override
    public int[] getItemLayouts() {
        return new int[]{R.layout.pay_dialog_adapter};
    }

    @Override
    public void onBindRecycleViewHolder(BaseRecyclerViewHolder viewHolder, int position) {
        final Pay pay = getItem(position);

        RelativeLayout payItem = viewHolder.findViewById(R.id.pay_item);
        payItem.setSelected(selectPosition == position);   // 选中状态

        TextView pr_coin = viewHolder.findViewById(R.id.pr_coin);
        TextView pr_money = viewHolder.findViewById(R.id.pr_money);
        TextView pr_desc = viewHolder.findViewById(R.id.pr_desc);

        pr_coin.setText(String.valueOf(pay.getWwCoinCz() + pay.getWwCoinZs()));
        pr_money.setText(Html.fromHtml("&#165;").toString() + pay.getIntPrice());

        pr_desc.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(pay.getRemark())) {
            pr_desc.setVisibility(View.VISIBLE);
            pr_desc.setText(pay.getRemark());
        }

        viewHolder.findViewById(R.id.pay_to).setOnClickListener(v -> {
            // 屏蔽优惠券
            PickerDialogUtil.showPayBottomDialog(AppTinkerLike.getActivity(), pay);

            // 从优惠券页面跳转过来的充值
//                if (coupon != null) {
//                    PickerDialogUtil.showPayCouponDialog(AppTinkerLike.getActivity(), pay, coupon);
//                    return;
//                }
//
//                // 普通充值，走检测可用优惠券流程
//                LoadingDialog.show(AppTinkerLike.getActivity(), "正在获取订单信息...");
//                ModuleMgr.getWawaMgr().reqUserCoupon(pay.getId(), new RequestComplete() {
//                    @Override
//                    public void onRequestComplete(HttpResponse response) {
//                        LoadingDialog.closeLoadingDialog();
//                        if (!response.isOk()) { // 获取可用优惠券失败，直接弹支付方式弹框
//                            PickerDialogUtil.showPayBottomDialog(AppTinkerLike.getActivity(), pay);
//                            return;
//                        }
//                        CouponList list = (CouponList) response.getBaseData();
//                        if (list == null) {  // 没有可使用优惠券，直接弹支付方式弹框
//                            PickerDialogUtil.showPayBottomDialog(AppTinkerLike.getActivity(), pay);
//                            return;
//                        }
//                        List<Coupon> data = list.getCouponEnableList();
//                        if (data.size() <= 0) {
//                            PickerDialogUtil.showPayBottomDialog(AppTinkerLike.getActivity(), pay);
//                            return;
//                        }
//
//                        // 弹优惠券选择弹框
//                        PickerDialogUtil.showCouponBottomDialog(AppTinkerLike.getActivity(), data, pay);
//                    }
//                });
        });
    }

    @Override
    public int getRecycleViewItemType(int position) {
        return 0;
    }

    public void updateData(int positon) {
        this.selectPosition = positon;
        notifyDataSetChanged();
    }
}